from selenium import webdriver
 
# 启动 Chrome 浏览器
driver = webdriver.Chrome(executable_path="path/to/chromedriver")  # 替换为你的 ChromeDriver 路径
 
# 打开网页
driver.get("https://www.python.org")
 
# 打印网页标题
print(driver.title)
 
# 关闭浏览器
driver.quit()


from selenium import webdriver
from selenium.webdriver.common.by import By
 
driver = webdriver.Chrome()
 
# 打开 Google 搜索页面
driver.get("https://www.google.com")
 
# 查找搜索框元素
search_box = driver.find_element(By.NAME, "q")
 
# 在搜索框中输入内容并提交
search_box.send_keys("Python Selenium")
search_box.submit()
 
# 等待几秒查看结果（仅用于演示）
import time
time.sleep(5)
 
# 关闭浏览器
driver.quit()


# 示例 HTML
# <input id="username" name="user" class="input-class" type="text">
 
# 通过 ID 查找
element_by_id = driver.find_element(By.ID, "username")
 
# 通过 Name 查找
element_by_name = driver.find_element(By.NAME, "user")
 
# 通过 Class 查找
element_by_class = driver.find_element(By.CLASS_NAME, "input-class")
 
# 通过 CSS 选择器查找
element_by_css = driver.find_element(By.CSS_SELECTOR, "#username")
 
# 通过 XPath 查找
element_by_xpath = driver.find_element(By.XPATH, "//input[@id='username']")


from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
# 等待某个元素可见
element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "username"))
)